﻿using FPSClient.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml;

namespace FPSClient.bll
{
    public class CappInfoManager
    {
        private static volatile CappInfoManager _instance;
        private static readonly object syncRoot = new object();

        private QMFawTechnicsInfo _cappInfo = null;
        private string _currentPath = "";

        public string CurrentCappPath { get; }

        public delegate void OnCappInfoChangedHandler(QMFawTechnicsInfo oldCappInfo, QMFawTechnicsInfo newCappInfo);
        public event OnCappInfoChangedHandler OnCappInfoChanged;

        private CappInfoManager()
        {

        }

        public static CappInfoManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (syncRoot)
                    {
                        if (_instance == null)
                        {
                            _instance = new CappInfoManager();
                        }
                    }
                }
                return _instance;
            }
        }

        private XmlNode parseCappXml(string fileName)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(fileName);

                _currentPath = System.IO.Path.GetDirectoryName(fileName);

                string currentPath = System.IO.Path.GetDirectoryName(fileName);

                XmlNode technicsNode = xmlDoc.SelectSingleNode("technics");
                if (technicsNode != null)
                {
                    XmlNode node = technicsNode.SelectSingleNode("QMFawTechnicsInfo");
                    return node;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            return null;
        }

        public void SetCappInfo(string cappXmlFileName)
        {
            XmlNode xmlNode = parseCappXml(cappXmlFileName);

            if (xmlNode != null)
            {
                QMFawTechnicsInfo oldCappInfo = _cappInfo;
                _cappInfo = CappXmlParser.ParseQMFawTechnicsInfo(xmlNode);
                if (OnCappInfoChanged != null)
                {
                    OnCappInfoChanged(oldCappInfo, _cappInfo);
                }
            }
        }

        public QMFawTechnicsInfo GetCappInfo()
        {
            return _cappInfo;
        }
        public List<ProcedureInfo> GetCurrentProcedureInfos()
        {
            List<ProcedureInfo> list = new List<ProcedureInfo>();

            if (_cappInfo != null)
            {
                foreach (QMProcedureInfo step in _cappInfo.steps)
                {
                    ProcedureInfo stepInfo = new ProcedureInfo();
                    stepInfo.OID = step.OID;
                    stepInfo.StepNumber = step.stepNumber;
                    stepInfo.StepName = step.stepName;
                    foreach (QMProcedureInfo pace in step.paces)
                    {
                        ProcedureInfo paceInfo = new ProcedureInfo();
                        paceInfo.OID = pace.OID;
                        paceInfo.StepNumber = pace.stepNumber;
                        paceInfo.StepName = pace.stepName;
                        paceInfo.parts = GetPartInfos(pace);
                        paceInfo.materials = GetMaterialInfos(pace);
                        paceInfo.tools = GetToolInfos(pace);
                        paceInfo.equipments = GetEquipmentInfos(pace);
                        paceInfo.zuoyemiaoshus = GetzuoyemiaoshuInfos(pace);
                        stepInfo.paces.Add(paceInfo);
                    }
                    list.Add(stepInfo);
                }
            }

            return list;
        }

        public static List<ProcedureInfo> GetProcedureInfos(QMFawTechnicsInfo technicsInfo)
        {
            List<ProcedureInfo> list = new List<ProcedureInfo>();

            if (technicsInfo != null)
            {
                foreach (QMProcedureInfo step in technicsInfo.steps)
                {
                    ProcedureInfo stepInfo = new ProcedureInfo();
                    stepInfo.OID = step.OID;
                    stepInfo.StepNumber = step.stepNumber;
                    stepInfo.StepName = step.stepName;
                    foreach (QMProcedureInfo pace in step.paces)
                    {
                        ProcedureInfo paceInfo = new ProcedureInfo();
                        paceInfo.OID = pace.OID;
                        paceInfo.StepNumber = pace.stepNumber;
                        paceInfo.StepName = pace.stepName;
                        paceInfo.parts = GetPartInfos(pace);
                        paceInfo.materials = GetMaterialInfos(pace);
                        paceInfo.tools = GetToolInfos(pace);
                        paceInfo.equipments = GetEquipmentInfos(pace);
                        paceInfo.zuoyemiaoshus = GetzuoyemiaoshuInfos(pace);
                        stepInfo.paces.Add(paceInfo);
                    }
                    list.Add(stepInfo);
                }
            }

            return list;
        }
        public static List<PartInfo> GetPartInfos(QMProcedureInfo procedureInfo)
        {
            List<PartInfo> list = new List<PartInfo>();
            if (procedureInfo != null)
            {
                foreach (part item in procedureInfo.parts)
                {
                    PartInfo partInfo = new PartInfo();
                    partInfo.NAME = item.NAME;
                    partInfo.XINGHAO = item.XINGHAO;
                    float useCount = 0;
                    float.TryParse(item.USECOUNT, out useCount);
                    partInfo.USECOUNT = useCount;
                    partInfo.UNIT = item.UNIT;
                    list.Add(partInfo);
                }
            }
            return list;
        }
        public static List<MaterialInfo> GetMaterialInfos(QMProcedureInfo procedureInfo)
        {
            List<MaterialInfo> list = new List<MaterialInfo>();
            if (procedureInfo != null)
            {
                foreach (material item in procedureInfo.materials)
                {
                    MaterialInfo materialInfo = new MaterialInfo();
                    materialInfo.FULIAONAME = item.FULIAONAME;
                    materialInfo.FULIAOTYPE = item.FULIAOTYPE;
                    float useCount = 0;
                    float.TryParse(item.FULIAOUSECOUNT, out useCount);
                    materialInfo.FULIAOUSECOUNT = useCount;
                    materialInfo.FULIAOUNIT = item.FULIAOUNIT;
                    list.Add(materialInfo);
                }
            }
            return list;
        }
        public static List<ToolInfo> GetToolInfos(QMProcedureInfo procedureInfo)
        {
            List<ToolInfo> list = new List<ToolInfo>();
            if (procedureInfo != null)
            {
                foreach (tool item in procedureInfo.tools)
                {
                    ToolInfo toolInfo = new ToolInfo();
                    toolInfo.NAME = item.NAME;
                    toolInfo.TYPE = item.TYPE;
                    list.Add(toolInfo);
                }
            }
            return list;
        }
        public static List<EquipmentInfo> GetEquipmentInfos(QMProcedureInfo procedureInfo)
        {
            List<EquipmentInfo> list = new List<EquipmentInfo>();
            if (procedureInfo != null)
            {
                foreach (equipment item in procedureInfo.equipments)
                {
                    EquipmentInfo equipmentInfo = new EquipmentInfo();
                    equipmentInfo.NAME = item.NAME;
                    equipmentInfo.XINGHAO = item.XINGHAO;
                    list.Add(equipmentInfo);
                }
            }
            return list;
        }
        public static List<zuoyemiaoshuInfo> GetzuoyemiaoshuInfos(QMProcedureInfo procedureInfo)
        {
            List<zuoyemiaoshuInfo> list = new List<zuoyemiaoshuInfo>();
            if (procedureInfo != null)
            {
                foreach (zuoyemiaoshu item in procedureInfo.zuoyemiaoshus)
                {
                    zuoyemiaoshuInfo zuoyemiaoshuInfo = new zuoyemiaoshuInfo();
                    zuoyemiaoshuInfo.XUHAO = item.XUHAO;
                    zuoyemiaoshuInfo.zuoyemiaoshuName = item.NAME;
                    zuoyemiaoshuInfo.miaoshu = item.miaoshu;
                    zuoyemiaoshuInfo.zhuyishixiang = item.zhuyishixiang;
                    list.Add(zuoyemiaoshuInfo);
                }
            }
            return list;
        }
    }
}
